import {
  request
} from "../../requests/post.js";

Page({
  data: {
    currentIndex: 0, // 当前显示的页面索引
    articleList: [{
      // url: "https://www.zzhignore.com/jy/her2",
      // title: '测试魂牵梦萦㫖奇葩蝇魂牵梦萦晃日常是浊江城 晃械浊魂牵梦萦',
      // cover: 'https://www.zzhignore.com/uploads/33a6b9b3-e5ab-4709-970a-ac64980a26ce.jpg'
    }], // 公众号文章列表
      
  },
  onLoad() {
    // 模拟从后端接口获取公众号文章信息
    console.info("onload")
    this.fetchArticleList("zmyt");
  },
  switchMenu(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      currentIndex: index
    });

    if(index == 0 ) {
      this.fetchArticleList("zmyt");
    } else if( index == 1) {
      this.fetchArticleList("cbyt");
    }
  },
  swiperChange(e) {
    const index = e.detail.current;
    this.setData({
      currentIndex: index
    });

    if(index == 0 ) {
      this.fetchArticleList("zmyt");
    } else if( index == 1) {
      this.fetchArticleList("cbyt");
    }
  },

  fetchArticleList(articleType) {
    const that = this;
    request({
      url: '/article/findList?pkid=' + articleType,
      method: 'GET'
    }).then(result => {
      console.info(JSON.stringify(result.data))
      if (result.data.errorCode === 200) {
        that.setData({
          articleList: result.data.data
        });
      } else {
        wx.showToast({
          title: result.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    }).catch(err =>{
      console.error('获取文章列表失败:', err);
    })
  },
    
  navigateToArticle(e) {
    const url = e.currentTarget.dataset.url;
    // 跳转到公众号文章页面
    wx.navigateTo({
      url: `/pages/blind/webview?url=${encodeURIComponent(url)}`
    });
  }
});